<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">车间</label>
        <el-select v-model="query.facility" clearable placeholder="车间" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery">
          <el-option v-for="item in facilityes" :key="item.id" :label="item.facilityname" :value="item.facility" />
        </el-select>
        <label class="el-form-item-label">班组类型</label>
        <el-input v-model="query.groupType" clearable placeholder="班组类型" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">班组</label>
        <el-input v-model="query.teamGroup" clearable placeholder="班组" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">班组名称</label>
        <el-input v-model="query.groupName" clearable placeholder="班组名称" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">状态</label>
        <el-input v-model="query.status" clearable placeholder="状态" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="车间" prop="facility">
            <el-select v-model="form.facility" style="width: 370px;">
              <el-option
                v-for="item in facilityes"
                :key="item.id"
                :label="item.facilityname"
                :value="item.facility"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="组织" prop="organization">
            <el-input v-model="form.organization" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="班组类型">
            <el-select v-model="form.groupType" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.group_type"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="班组" prop="teamGroup">
            <el-input v-model="form.teamGroup" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="班组名称" prop="groupName">
            <el-input v-model="form.groupName" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="配置人数">
            <el-input v-model="form.configuredNumber" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="默认班次">
            <el-select v-model="form.defaultShift" style="width: 370px;">
              <el-option
                v-for="item in shiftes"
                :key="item.id"
                :label="item.shiftName"
                :value="item.shift"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="form.remarks" :rows="3" type="textarea" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-radio v-for="item in dict.group_status" :key="item.id" v-model="form.status" :label="item.value">{{ item.label }}</el-radio>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="facility" label="车间" />
        <el-table-column prop="organization" label="组织" />
        <el-table-column prop="groupType" label="班组类型">
          <template slot-scope="scope">
            {{ dict.label.group_type[scope.row.groupType] }}
          </template>
        </el-table-column>
        <el-table-column prop="teamGroup" label="班组" />
        <el-table-column prop="groupName" label="班组名称" />
        <el-table-column prop="configuredNumber" label="配置人数" />
        <el-table-column prop="shifts" :show-overflow-tooltip="true" label="默认班次">
          <template slot-scope="scope">
            {{ scope.row.shifts.shiftName }}
          </template>
        </el-table-column>
        <el-table-column prop="remarks" label="备注" />
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            {{ dict.label.group_status[scope.row.status] }}
          </template>
        </el-table-column>
        <el-table-column v-if="checkPer(['admin','group:edit','group:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudGroup from '@/api/system/group'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import { getFacilitys } from '@/api/system/facility'
import { getShifts } from '@/api/system/shift'

const defaultForm = { id: null, facility: null, organization: null, groupType: null, teamGroup: null, groupName: null, configuredNumber: null, defaultShift: null, remarks: null, status: null, shifts: { id: null }, facilitys: { id: null }}
export default {
  name: 'Group',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['group_type', 'group_status'],
  cruds() {
    return CRUD({ title: '系统：班组', url: 'api/group', idField: 'id', sort: 'id,desc', crudMethod: { ...crudGroup }})
  },
  data() {
    return {
      facilityes: [],
      shiftes: [],
      permission: {
        add: ['admin', 'group:add'],
        edit: ['admin', 'group:edit'],
        del: ['admin', 'group:del']
      },
      rules: {
        facility: [
          { required: true, message: '车间不能为空', trigger: 'blur' }
        ],
        organization: [
          { required: true, message: '组织不能为空', trigger: 'blur' }
        ],
        teamGroup: [
          { required: true, message: '班组不能为空', trigger: 'blur' }
        ],
        groupName: [
          { required: true, message: '班组名称不能为空', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '状态不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'facility', display_name: '车间' },
        { key: 'organization', display_name: '组织' },
        { key: 'groupType', display_name: '班组类型' },
        { key: 'teamGroup', display_name: '班组' },
        { key: 'groupName', display_name: '班组名称' },
        { key: 'configuredNumber', display_name: '配置人数' },
        { key: 'defaultShift', display_name: '默认班次' },
        { key: 'remarks', display_name: '备注' },
        { key: 'status', display_name: '状态' }
      ]
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      this.getFacility()
      this.getShift()
      return true
    },
    [CRUD.HOOK.beforeToEdit](crud, form) {
      this.handleStatus(form)
    },
    getFacility() {
      getFacilitys({ enabled: true }).then(res => {
        const facilityList = res.content
        this.bulidFacility(facilityList)
        this.facilityes = facilityList
      }).catch(() => {})
    },
    bulidFacility(facilityList) {
      facilityList.forEach(data => {
        data.facilityname = data.facility + '-' + data.facilityname
      })
    },
    getShift() {
      getShifts({ enabled: true }).then(res => {
        this.shiftes = res.content
      })
    },
    handleStatus(form) {
      this.form.status = form.status.toString()
    }
  }
}
</script>

<style scoped>

</style>
